define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            Table.api.init({
                extend: {
                    index_url: 'scanwork/bom/index' + location.search,
                    add_url: 'scanwork/bom/add',
                    edit_url: 'scanwork/bom/edit',
                    del_url: 'scanwork/bom/del',
                    multi_url: 'scanwork/bom/multi',
                    table: 'scanwork_bom',
                }
            });

            var table = $("#table");

            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID', sortable: true},
                        {field: 'bom_no', title: 'BOM编号', operate: 'LIKE'},
                        {field: 'bom_name', title: 'BOM名称', operate: 'LIKE'},
                        {field: 'product.name', title: '产品'},
                        {field: 'version', title: '版本'},
                        {field: 'bom_type_text', title: 'BOM类型'},
                        {field: 'status_text', title: '状态', searchList: {0:'草稿',1:'审核中',2:'已发布',3:'已归档'}},
                        {field: 'creator_name', title: '创建人'},
                        {field: 'createtime', title: '创建时间', operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, 
                            formatter: Table.api.formatter.operate, 
                            buttons: [
                                {
                                    name: 'items',
                                    text: 'BOM明细',
                                    title: 'BOM明细',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'scanwork/bom/items'
                                },
                                {
                                    name: 'copy',
                                    text: '复制',
                                    title: '复制BOM',
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    icon: 'fa fa-copy',
                                    url: 'scanwork/bom/copy',
                                    confirm: '确认复制此BOM吗？'
                                }
                            ]
                        }
                    ]
                ]
            });

            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

